package com.wonder.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.BdPda;
import com.wonder.app.mapper.BdPdaDao;
import com.wonder.app.service.BdPdaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class BdPdaServiceImpl extends ServiceImpl<BdPdaDao, BdPda> implements BdPdaService {
    @Autowired
    private BdPdaDao bdPdaDao;

    @Autowired
    private BdPdaService bdPdaService;
    /**
     * 根据uuid获取蓝牙地址
     * @param map
     * @return
     */
    @Override
    public Map<String,Object> getMacByUuid(Long user_id,Map<String, Object> map) throws Exception {
        String uuid = VtuzxUtil.getString(map,"uuid");
        BdPda bdPda = bdPdaDao.selectOne(new LambdaQueryWrapper<BdPda>()
                .eq(BdPda::getUuid, uuid));
        if (VtuzxUtil.isEmpty(bdPda)){
            throw new RuntimeException("pda没有绑定打印机，请联系管理员");
        }
        Map<String,Object> data = new HashMap<>();
        data.put("macAdress",bdPda.getMacAddress());
        return data;
    }

    /**
     * 打印机和pda绑定
     * @param userId
     * @param map
     * @param userName
     * @return
     * @throws Exception
     */
    @Override
    public Boolean bandingMacByUuid(Long userId, Map<String, Object> map, String userName) throws Exception {
        String printerAddr = VtuzxUtil.getString(map,"printerAddr");
        String uuid = VtuzxUtil.getString(map,"uuid");
        //根据uuid查询有没有，有的话修改，没有的话新增
        BdPda bdPda = bdPdaDao.selectOne(new LambdaQueryWrapper<BdPda>()
                .eq(BdPda::getUuid, uuid));
        if(!VtuzxUtil.isEmpty(bdPda)){//有这个pda的uuid
            bdPda.setMacAddress(printerAddr);
            bdPda.setUserName(userName);
        } else {
            bdPda = new BdPda();
            bdPda.setMacAddress(printerAddr);
            bdPda.setUuid(uuid);
            bdPda.setUserName(userName);
        }
        boolean b = bdPdaService.saveOrUpdate(bdPda);

        return b;
    }
}
